<?php
namespace app\controller\common;

use app\common\controller\_BaseController;
use app\model\Admin;
use app\model\Token;
use app\model\Config;
use think\Validate;

class LoginController extends _BaseController
{   
	protected $rule = [
		'name|登录账号'    		=> 'alphaDash|length:3,50',
		'password|登录密码'     	=> 'alphaDash|length:3,64',
		'type|类型'				=> 'in:1,2',
		'verifycode|验证码'		=> 'alphaNum|length:4|captcha',
		'verifykey|验证码标识'	=> 'requireWith:verifycode|length:1,64|alphaNum',
	];

	protected $scene = [
		'index'	=> ['name','password','type','verifycode','verifykey'],
		'out'	=> [],
	];

	protected function initialize()
	{
		parent::initialize();
		switch ($this->action) {
			case 'index':
				$this->appendRule('name','require');
				$this->appendRule('password','require');
				$this->appendRule('type','require');
				$this->appendRule('verifycode','requireCallback:checkRequire');
				$this->verify();
				break;
			default:
				# code...
				break;
		}
		$this->check();
	}
	/**
	 * @api {POST} /common/login 登录
	 * @apiVersion 0.0.1
	 * @apiGroup Common
	 *
	 * @apiParam {String} name 名称
	 * @apiParam {String} password 密码
	 * @apiParam {Number=1管理员,2用户} type类型
	 * @apiParam {String} [verifycode] 验证码
	 * @apiParam {String} [verifykey] 验证码标识
	 * 
	 * @apiParamExample {json} Request-Example:
	 * {
	 *     "name": "ad222",
	 *     "password": "1234561",
	 *     "type": "1",
	 *     "verifycode": "1123",
	 *     "verifykey": "f59bd65f7edafb087a81d4dca06c4910",
	 * }
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": {
	 *         "token": "142a091442a0647df4a7c39c1cab07e2"
	 *     }
	 * }
	 */
	public function indexAction()
	{   

		$user = Token::getUserByName($this->data['name'],$this->data['type']);
		if($user && $user->password == md5($this->data['password'])){
	        $user->last_login_time = time();
	        $user->save();
	        $key = config('params.token_field');
	        $token = Token::doCreate($user->id,$this->data['type']);
	        $res = [$key => $token];
	        $user['type'] = $this->data['type'];
	        $this->user = $user;
	        return parent::resSuccess($res);
		}
		return parent::resError('账号或密码错误');
	}

	/**
	 * @api {POST} /common/login/out 登出
	 * @apiVersion 0.0.1
	 * @apiGroup Common
	 *
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []
	 * }
	 */
	public function OutAction()
	{   
		$token = request()->param(config('params.token_field'));
    	$token = Token::getToken($token);
        if(!$token) parent::resError('无效token！',1001);
        $user = Token::getUserById($token->user_id,$token->user_type);
        $user['type'] = $token->user_type;
    	$this->user = $user;
		Token::doDelete($this->token);
		return parent::resSuccess();
	}

	// 自定义规则
	protected function checkRequire()
	{
		Validate::extend('checkRequire', function () {
			$is_verify = Config::where(['alias'=>'admin_login_verify'])->find()->value;
			if($is_verify == 1) return true;
		});
	}
}
